﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLTV
{
    public partial class frmQuanLyDocGia : Form
    {
        public frmQuanLyDocGia()
        {
            InitializeComponent();
        }

        private void frmQuanLyDocGia_Load(object sender, EventArgs e)
        {
            int  maDG =  DocGiaService.LayMaDocGiaMax();
            maDG += 1;
            this.txtMaTheDG.Text =DocGiaService.PhatSinhMaTheDocGia(maDG.ToString());
            List<LoaiDocGiaDTO> list = LoaiDocGiaService.LayDanhSachLoaiDocGia();

            cmbLoaiDocGia.DisplayMember = LoaiDocGiaColums.TenLoaiDocGia;
            cmbLoaiDocGia.ValueMember = LoaiDocGiaColums.MaLoaiDocGia;
            cmbLoaiDocGia.DataSource = list;

            txtNguoiLap.Text = ThongTinDangNhapService.tenNV;
            btnTimDocGia.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;

        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {            
            DocGiaDTO dgDTO = new DocGiaDTO();
            dgDTO.MaTheDocGia = txtMaTheDG.Text;
            dgDTO.HoTen = txtHoTen.Text;
            int ngay = Convert.ToInt32(cmbNgay.SelectedItem);
            int thang = Convert.ToInt32(cmbThang.SelectedItem);
            int nam = Convert.ToInt32(txtNam.Text);
            dgDTO.NgaySinh = new DateTime(nam, thang, ngay);
            dgDTO.DiaChi = txtDiaChi.Text;
            dgDTO.Email = txtEmail.Text;            
            dgDTO.MaLoaiDocGia =Convert.ToInt32(cmbLoaiDocGia.SelectedValue );
            dgDTO.NgayLapThe = Convert.ToDateTime(dtpNgayLap.Text);
            dgDTO.NguoiLap = ThongTinDangNhapService.maNV;            
            
            int hanNgay = dgDTO.NgayLapThe.Day;
            int hanThang = dgDTO.NgayLapThe.Month+6;
            int hanNam = dgDTO.NgayLapThe.Year;
            if (hanThang > 12)
            {
                hanThang = hanThang - 12;
                hanNam = nam + 1;
            }
            
            dgDTO.ThoiHanThe = new DateTime(hanNam, hanThang,hanNgay);
            dgDTO.SoSachDangMuon = 0;
            dgDTO.TienNo = (float)0;
            dgDTO.DaXoa = 0;
            DocGiaService.ThemDocGia(dgDTO);
            MessageBox.Show("Lap the thanh cong!");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdbLapThe_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbLapThe.Checked == true)
            {
                lblMaThe.Text = "Mã thẻ độc giả:";
                btnTimDocGia.Enabled = false;
                btnThucHien.Enabled = true;
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = false;
                int maDG = DocGiaService.LayMaDocGiaMax();
                maDG += 1;
                this.txtMaTheDG.Text = DocGiaService.PhatSinhMaTheDocGia(maDG.ToString());
                this.txtMaTheDG.ReadOnly = true;
                this.txtNguoiLap.ReadOnly = false;
                this.txtHoTen.ReadOnly = false;
                this.txtDiaChi.ReadOnly = false;
                txtEmail.ReadOnly = false;
                txtNam.ReadOnly = false;
                cmbNgay.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbThang.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbLoaiDocGia.DropDownStyle = ComboBoxStyle.DropDown;
                dtpNgayLap.Enabled = true;
            }
        }

        private void rdbCapNhat_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbCapNhat.Checked == true)
            {
                lblMaThe.Text = "Mã độc giả:";
                txtMaTheDG.Text = "";
                txtHoTen.Text = "";
                txtEmail.Text = "";
                txtDiaChi.Text = "";
                txtNguoiLap.Text = "";
                btnTimDocGia.Enabled = true;
                btnThucHien.Enabled = false;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = false;
                this.txtMaTheDG.ReadOnly = false;               
                this.txtNguoiLap.ReadOnly = true;
                this.txtHoTen.ReadOnly = false;
                this.txtDiaChi.ReadOnly = false;
                txtEmail.ReadOnly = false;
                txtNam.ReadOnly = true;
                txtNam.Text = "";
                //cmbNgay.DisplayMember = "";
                //cmbThang.Text = "";
                cmbNgay.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbThang.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbLoaiDocGia.DropDownStyle = ComboBoxStyle.DropDownList;
                dtpNgayLap.Enabled = false;
            }
        }

        private void rdbXoa_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbXoa.Checked == true)
            {
                lblMaThe.Text = "Mã độc giả:";
                txtMaTheDG.Text = "";
                txtHoTen.Text = "";
                txtEmail.Text = "";
                txtDiaChi.Text = "";
                txtNguoiLap.Text = "";
                btnTimDocGia.Enabled = true;
                btnThucHien.Enabled = false;
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = true;
                this.txtMaTheDG.ReadOnly = false;
                this.txtNguoiLap.ReadOnly = true;
                this.txtHoTen.ReadOnly = true;
                this.txtDiaChi.ReadOnly = true;
                txtEmail.ReadOnly = true;
                txtNam.ReadOnly = true;
                txtNam.Text = "";
                //cmbNgay.Text = "";
                //cmbThang.Text = "";
                cmbNgay.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbThang.DropDownStyle = ComboBoxStyle.DropDownList;                
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int maDocGia = Convert.ToInt32(txtMaTheDG.Text);
            DocGiaDTO docGiaDTO = DocGiaService.LayThongTinDocGia(maDocGia);
            docGiaDTO.HoTen = txtHoTen.Text;
            int ngay = Convert.ToInt32(cmbNgay.SelectedItem);
            int thang = Convert.ToInt32(cmbThang.SelectedItem);
            int nam = Convert.ToInt32(txtNam.Text);
            docGiaDTO.NgaySinh = new DateTime(nam, thang, ngay);
            docGiaDTO.DiaChi = txtDiaChi.Text;
            docGiaDTO.Email = txtEmail.Text;

            DocGiaService.CapNhatDocGia(docGiaDTO);
        }

        private void txtMaTheDG_TextChanged(object sender, EventArgs e)
        {
            try
            {                
                    int maDocGia = Convert.ToInt32(this.txtMaTheDG.Text);
                    DocGiaDTO docGiaDTO = DocGiaService.LayThongTinDocGia(maDocGia);
                    txtHoTen.Text = docGiaDTO.HoTen;
                    txtDiaChi.Text = docGiaDTO.DiaChi;
                    txtEmail.Text = docGiaDTO.Email;
                    dtpNgayLap.Text = Convert.ToString(docGiaDTO.NgayLapThe);
                    int ngay = docGiaDTO.NgaySinh.Day;
                    int thang = docGiaDTO.NgaySinh.Month;
                    int nam = docGiaDTO.NgaySinh.Year;
                    cmbNgay.Text = ngay.ToString();
                    cmbThang.Text = thang.ToString();
                    txtNam.Text = nam.ToString();
                    txtNguoiLap.Text = ThongTinDangNhapService.tenNV;

                    LoaiDocGiaDTO loaiDG = LoaiDocGiaService.LayThongTinLoaiDocGia(docGiaDTO.MaLoaiDocGia);
                    cmbLoaiDocGia.Text = loaiDG.TenLoaiDocGia;                
            }
            catch (System.Exception)
            { 
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (rdbXoa.Checked == true)
                {
                    int maTheDocGia = Convert.ToInt32(txtMaTheDG.Text);
                    DocGiaDTO docGiaDTO = DocGiaService.LayThongTinDocGia(maTheDocGia);
                    docGiaDTO.DaXoa = 1;

                    DocGiaService.XoaDocGia(maTheDocGia, docGiaDTO);
                    MessageBox.Show("Xoa doc gia thanh cong!");
                }
            }
            catch(System.Exception)
            { 

            }
        }

        private void btnTimDocGia_Click(object sender, EventArgs e)
        {
            frmTraCuuDocGia frmTraCuu = new frmTraCuuDocGia();
            frmTraCuu.Show();
        }
    }
}
